package com.xiaoshuidi.cloud.module.contract.dal.mysql.contract;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshuidi.cloud.framework.mybatis.core.mapper.BaseMapperX;
import com.xiaoshuidi.cloud.module.contract.controller.admin.bill.vo.CompanyCustomerBillPageResp;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.contract.CustomerCompanyDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 企业客户 Mapper
 *
 * @author 合房租赁
 */
@Mapper
public interface CustomerCompanyMapper extends BaseMapperX<CustomerCompanyDO> {

    /**
     * 企业客户 房屋账单列表
     *
     * @param page
     * @param wrapper
     * @return
     */
    @Select("select cr.id, " +
            "       cr.room_name, " +
            "       cr.deposit, " +
            "       cr.total_rent, " +
            "       cr.order_share_method " +
            " from contract_room cr " +
            " left join contract_company cc on cr.company_contract_id = cc.id " +
            " where ${ew.sqlSegment}")
    Page<CompanyCustomerBillPageResp> customerCompanyBillPage(Page<?> page, @Param("ew") QueryWrapper<?> wrapper);

    /**
     * 查询合同账单已支付期数、总期数
     *
     * @param
     * @return
     */
    @Select("select business_id id,  count(num) periods, " +
            "       sum(case when pay_status = 'payment' then 1 " +
            "           else 0 end) paymentPeriod " +
            "from contract_rentorder cr " +
            "where ${ew.sqlSegment} " +
            "group by business_id;")
    List<CompanyCustomerBillPageResp> billPeriod(@Param("ew") QueryWrapper<?> wrapper);
}
